﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using System.Collections.Generic;
using PowerCreator.LMS.Course.Entity;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.Config;



public partial class Theme_3_SearchCourse : PowerCreator.LMS.Web.Base.WebBasePage
{


    ManagerPaper opPaper = new ManagerPaper();
    PowerCreator.LMS.Web.Course.Course opCourse = new PowerCreator.LMS.Web.Course.Course();
    PowerCreator.LMS.Web.Catalog.Catalog opCatalog = new PowerCreator.LMS.Web.Catalog.Catalog();
    PowerCreator.LMS.Web.Course.Resource opResource = new PowerCreator.LMS.Web.Course.Resource();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = config.GetLanagerValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        if (!IsPostBack)
        {
            ViewState["CourseName"] = "";
            if (Request.QueryString["CourseName"] != null)
            {
                ViewState["CourseName"] =PowerCreator.LMS.Common.Utils.Base64.Decode(Request.QueryString["CourseName"].ToString());
                txt_CourseName.Text = ViewState["CourseName"].ToString();
            }
            ViewState["ResourceName"] = "";
            if (Request.QueryString["ResourceName"] != null)
            {
                ViewState["ResourceName"] = PowerCreator.LMS.Common.Utils.Base64.Decode(Request.QueryString["ResourceName"].ToString());
                txt_ResourceName.Text = ViewState["ResourceName"].ToString();
            }
            //BinderCatalog();
            //BinderNewCourse();

            string search = GetSearchOnLoad();
            //Response.Write(search);
            //Response.End();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }

    protected override void InitPage()
    {
        btn_Search.Text = config.GetLanagerValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }

    private string GetSearchOnLoad()
    {
        StringBuilder sbSearch = new StringBuilder();

        if (ViewState["CourseName"].ToString() !="")
        {
            sbSearch.Append(string.Format(" CourseName like '%{0}%' and ", ViewState["CourseName"].ToString()));
        }

        if (ViewState["ResourceName"].ToString() != "")
        {
            sbSearch.Append(string.Format("  (CourseID in (SELECT TCourseResource.FKCourseresCourseID FROM TResource INNER JOIN TCourseResource ON  TResource.ResourceID =  TCourseResource.FKCoursersResourceID where TResource.ResourceName like '%{0}%' ) or CourseID in (SELECT TCourseResource.FKCourseresCourseID FROM Paper INNER JOIN TCourseResource ON  Paper.PaperID =  TCourseResource.FKCoursersPaperID where Paper.PaperName like '%{1}%')) and", ViewState["ResourceName"].ToString(), ViewState["ResourceName"].ToString()));
        }

        sbSearch.Append(" CourseState = 1  ");
        return sbSearch.ToString();
    }


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        if (!string.IsNullOrEmpty(txt_CourseName.Text))
        {
            sbSearch.Append(string.Format(" CourseName like '%{0}%' and ", txt_CourseName.Text));
        }
        if (!string.IsNullOrEmpty(txt_ResourceName.Text))
        {
            sbSearch.Append(string.Format("  (CourseID in (SELECT TCourseResource.FKCourseresCourseID FROM TResource INNER JOIN TCourseResource ON  TResource.ResourceID =  TCourseResource.FKCoursersResourceID where TResource.ResourceName like '%{0}%' ) or CourseID in (SELECT TCourseResource.FKCourseresCourseID FROM Paper INNER JOIN TCourseResource ON  Paper.PaperID =  TCourseResource.FKCoursersPaperID where Paper.PaperName like '%{1}%')) and ", txt_ResourceName.Text, txt_ResourceName.Text));
        }

        sbSearch.Append(" CourseState = 1 ");
        return sbSearch.ToString();
    }

    private void BinderPager(string search)
    {
        int count = 2;

        PowerCreator.LMS.SCR.Dog.GetMaxUserCount();
        if (PowerCreator.LMS.SCR.Dog.CheckDog())
        {
            AspNetPager1.PageSize = PowerCreator.LMS.Web.Config.WebConfig.PageSize;
            count = opCourse.QueryCourseCount(search);
            
        }
        else
        {
            AspNetPager1.PageSize = count;
        }
        AspNetPager1.RecordCount = count;

        PreAspNetPager(AspNetPager1);
    }

    private void BinderRPT(string search, int pageIndex)
    {
        string orderby = " CourseCreatetime desc,CourseId desc";
        
        DataTable dt = opCourse.QueryCourse(search,orderby, PowerCreator.LMS.Web.Config.WebConfig.PageSize, pageIndex);


        if (!PowerCreator.LMS.SCR.Dog.CheckDog())
        {

            for (int i = dt.Rows.Count - 1; i > 1; i--)
            {
                dt.Rows[i].Delete();
            }

        }

        rpt_CourseList.DataSource = dt;
        rpt_CourseList.DataBind();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ViewState["CourseID"] = "0";
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rpt_ResourceList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    }

    //DataTable为多表联链接查询后的数据
    private List<CourseResourceInfo> FillList(DataTable dt)
    {
        List<CourseResourceInfo> list = new List<CourseResourceInfo>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            CourseResourceInfo item = new CourseResourceInfo();
            item.CourseID = int.Parse(dt.Rows[i]["FKCourseresCourseID"].ToString());
            item.CourseresID = int.Parse(dt.Rows[i]["CourseresID"].ToString());
            item.CoursersRestpID = int.Parse(dt.Rows[i]["FKCoursersRestpID"].ToString());
            

            VOTResourceType voResourceType = opCourse.LoadResourceType(item.CoursersRestpID);
            item.CoursersRestpName = voResourceType.RestpName;
            item.CoursersClassID = int.Parse(dt.Rows[i]["CoursersClass"].ToString());
            item.CoursersClassName = opCourse.GetCoursersClassByData(item.CoursersClassID, GetLanageValueByKey("Resource_Class1", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("Resource_Class2", PowerCreator.LMS.Language.LanagetItemType.Page));

            if (item.CoursersClassID == Convert.ToInt16(CoursersClass.Courseware))  //课件资源
            {
                item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersResourceID"].ToString());
                VOTResource voResource = opCourse.LoadResource(item.CoursersResourceID);
                item.ResourceName = voResource.ResourceName;
                item.CoursersResourceAuthor = voResource.ResourceAuthor;
            }
            else if (item.CoursersClassID == Convert.ToInt16(CoursersClass.Paper)) //课程资源
            {
                item.CoursersResourceID = int.Parse(dt.Rows[i]["FKCoursersPaperID"].ToString());
                VOPaper voPaper = opPaper.LoadPaper(item.CoursersResourceID);
                item.ResourceName = voPaper.PaperName;
            }

            list.Add(item);
        }
        return list;
    }

    protected void rpt_CourseList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "lshow")
        {
            for (int i = 0; i < rpt_CourseList.Items.Count; i++)
            {
                Panel pl_ResourceList = (Panel)rpt_CourseList.Items[i].FindControl("pl_ResourceList");
                pl_ResourceList.Visible = false;
            }
            Panel pl_ResourceList1 = (Panel)e.Item.FindControl("pl_ResourceList");
            pl_ResourceList1.Visible = true;
            Repeater rptResource = (Repeater)e.Item.FindControl("rpt_ResourceList");
            //课件类型的课程资源

            string search = "FKCourseresCourseID =" + e.CommandArgument.ToString();
            DataTable dt = opCourse.QueryCourseResource(search);
            List<CourseResourceInfo> list = FillList(dt);

            int cout = list.Count;
            if (cout > 10)
            {
                if (!PowerCreator.LMS.SCR.Dog.CheckDog())
                {
                    list.RemoveRange(10, cout - 10);

                }
            }


            if (dt.Rows.Count != 0)
            {
                rptResource.DataSource = list;
                rptResource.DataBind();
            }
            else
            {
                pl_ResourceList1.Visible = false;
            }



            //string search = string.Format(" ResourceID in (select FKCoursersResourceID from TCourseResource where FKCourseresCourseID={0} ) and ResourseState = 1", e.CommandArgument);
            //DataTable dt = opResource.QueryResource(search, opResource.QueryResourceCount(search), 1);
            //if (dt.Rows.Count != 0)
            //{
            //    dt.Columns.Add("ResourceSysType1");
            //    dt.Columns.Add("OpenUrl1");
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        dt.Rows[i]["ResourceSysType1"] = opCourse.GetSysResourceTypeByValue(int.Parse(dt.Rows[i]["ResourceClass"].ToString()));
            //        dt.Rows[i]["OpenUrl1"] = "OpenCourseware.aspx?ResourceID=" + dt.Rows[i]["ResourceID"].ToString();
            //    }
            //    rptResource.DataSource = dt;
            //    rptResource.DataBind();
            //}
            //else
            //{
            //    pl_ResourceList1.Visible = false;
            //}
        }

    }
}
